/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (xianliru@163.com)
 */

package com.pig4cloud.pig.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.admin.api.dto.AreaTree;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.admin.api.entity.Area;
import com.pig4cloud.pig.admin.service.AreaService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;


/**
 * 中国地区
 *
 * @author xianliru@163.com
 * @date 2020-04-23 23:17:09
 */
@RestController
@AllArgsConstructor
@RequestMapping("/area" )
@Api(value = "area", tags = "中国地区管理")
public class AreaController {

    private final  AreaService areaService;

    /**
     * 查询中国地区直辖市省集合
     * @return R
     */
    @ApiOperation(value = "获取直辖市省集合", notes = "获取直辖市省集合")
    @GetMapping("/province" )
    public R<List <Area>> getProvince() {
        List <Area> province = areaService.getProvince();
        return R.ok(province);
    }

    /**
     * 查询中国地区指定地区code下的子集
     * @return R
     */
    @ApiOperation(value = "指定地区code下的子集", notes = "指定地区code下的子集")
    @GetMapping("/{code}" )
    public R<List <Area>> getProvince(@PathVariable Long code) {
        List <Area> childrens = areaService.getChildrensByParentCode(code);
        return R.ok(childrens);
    }

    /**
     * 查询地区详情信息
     * @return R
     */
    @ApiOperation(value = "指定地区code下的子集", notes = "指定地区code下的子集")
    @GetMapping("/details" )
    public R<Area> getDetailsById(@RequestParam("id") Long id) {
        Area area = areaService.getById(id);
        return R.ok(area);
    }

    /**
     * 返回树形菜单集合
     *
     * @return 树形菜单
     */
    @GetMapping(value = "/tree")
    public R<List<AreaTree>> listDeptTrees() {
        return R.ok(areaService.getAreaTree());
    }

    /**
     * 新增中国地区
     * @param area 中国地区
     * @return R
     */
    @ApiOperation(value = "新增中国地区", notes = "新增中国地区")
    @SysLog("新增中国地区" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('user_area_add')" )
    public R save(@RequestBody Area area) {
        return R.ok(areaService.save(area));
    }

    /**
     * 修改中国地区
     * @param area 中国地区
     * @return R
     */
    @ApiOperation(value = "修改中国地区", notes = "修改中国地区")
    @SysLog("修改中国地区" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('user_area_edit')" )
    public R updateById(@RequestBody Area area) {
        return R.ok(areaService.updateById(area));
    }

    /**
     * 通过id删除中国地区
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除中国地区", notes = "通过id删除中国地区")
    @SysLog("通过id删除中国地区" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('user_area_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(areaService.removeById(id));
    }

}
